/*
 * Copyright (C) 2009, 2010, B3log Team
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.b3log.nide.service.maven;

import java.io.File;
import java.util.logging.Logger;
import org.b3log.latke.service.ServiceException;
import org.b3log.nide.Nide;
import org.b3log.nide.action.websocket.OutputSocketAction;
import org.b3log.nide.service.maven.cmd.AbstractMvnCommand;
import org.b3log.nide.service.maven.cmd.CleanCmd;
import org.b3log.nide.service.maven.cmd.ExecCmd;
import org.b3log.nide.service.maven.cmd.GenSampleProjectCmd;
import org.b3log.nide.service.maven.cmd.InstallCmd;
import org.json.JSONObject;

/**
 * <a href="http://maven.apache.org">Maven</a> service.
 *
 * @author <a href="mailto:DL88250@gmail.com">Liang Ding</a>
 * @version 1.0.0.8, Mar 10, 2011
 */
public final class MavenManager {

    /**
     * Logger.
     */
    private static final Logger LOGGER =
            Logger.getLogger(MavenManager.class.getName());
    /**
     * Instance.
     */
    public static final MavenManager INSTANCE = new MavenManager();

    /**
     * Private default constructor.
     */
    private MavenManager() {
    }

    /**
     * Gets the path of mvn command. The end of the path with a whitespace.
     *
     * @return the path of mvn command
     */
    public static String getMvnPath() {
        final String path = getMavenHomePath() + "bin" + File.separator;

        if (Nide.isWindows()) {
            return path + "mvn.bat ";
        } else {
            return path + "mvn ";
        }
    }

    /**
     * Gets the path of Maven home.
     *
     * @return the path of Maven home
     */
    public static String getMavenHomePath() {
        return Nide.getNideProperties().getString("maven.home");
    }

    /**
     * Generates a quickstart sample Maven project with the specified project
     * initialize parameters.
     *
     * @param projectInitParams project initialize parameters, for example:
     * <pre>
     * {
     *     "projectParentDir": "/home/daniel/nide/projects"
     *     "projectGroupId": "org.b3log",
     *     "projectArtifactId": "l0y0l",
     *     "projectVersion": "1.0.0"
     * }
     * </pre>
     * @return for example:
     * <pre>
     * TODO
     * </pre>
     * @throws ServiceException service exception
     */
    public JSONObject genSampleProject(final JSONObject projectInitParams)
            throws ServiceException {
        final AbstractMvnCommand command = new GenSampleProjectCmd();

        final JSONObject ret = command.execute(projectInitParams);

        return ret;
    }

    /**
     * Cleans the specified Maven project.
     *
     * @param project the specified Maven project, for example:
     * <pre>
     * {
     *     "projectBaseDir": "/home/daniel/nide/projects/nide-test/"
     * }
     * </pre>
     * @return for example:
     * <pre>
     * {
     * TODO
     * }
     * </pre>
     * @throws ServiceException service exception
     */
    public JSONObject cleanProject(final JSONObject project)
            throws ServiceException {
        final AbstractMvnCommand command = new CleanCmd();
        final JSONObject ret = command.execute(project,
                                               OutputSocketAction.OUTPUT_SOCKETS.
                peek());

        return ret;
    }

    /**
     * Installs the specified Maven project.
     *
     * @param project the specified Maven project, for example:
     * <pre>
     * {
     *     "projectBaseDir": "/home/daniel/nide/projects/nide-test/"
     * }
     * </pre>
     * @return for example:
     * <pre>
     * {
     * TODO
     * }
     * </pre>
     * @throws ServiceException service exception
     */
    public JSONObject installProject(final JSONObject project)
            throws ServiceException {
        final AbstractMvnCommand command = new InstallCmd();
        final JSONObject ret = command.execute(
                project, OutputSocketAction.OUTPUT_SOCKETS.peek());

        return ret;
    }

    /**
     * Runs the specified Java file.
     *
     * @param javaFile the specified Java file, for example:
     * <pre>
     * {
     *     "projectBaseDir": "/home/daniel/nide/projects/nide/"
     *     "mainClass": "org.b3log.nide.Nide"
     * }
     * </pre>
     * @return for example:
     * <pre>
     * {
     * TODO
     * }
     * </pre>
     * @throws ServiceException service exception
     */
    public JSONObject runJavaFile(final JSONObject javaFile)
            throws ServiceException {
        final AbstractMvnCommand command = new ExecCmd();
        final JSONObject ret = command.execute(javaFile,
                                               OutputSocketAction.OUTPUT_SOCKETS.
                pop());

        return ret;
    }
}
